package de.pierrepoenisch.checkyoursite.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.KeyboardListenerAdapter;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import de.pierrepoenisch.checkyoursite.client.SortableFlexTable.SortDirectionET;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CheckYourSite implements EntryPoint {
    final Button button = new Button("Check Site");
    final TextBox UriTB = new TextBox();
    final SortableFlexTable resultTA = new SortableFlexTable();
    final Button upFileBT = new Button("File");

  /**
   * This is the entry point method.
   */
  public void onModuleLoad() {

    button.addClickListener(new ClickListener() {
    	public void onClick(Widget sender) {
        requestCheckSite();
      }
    });    
    
    //Keyboardlistener for the text box that starts the request    
    UriTB.addKeyboardListener(new KeyboardListenerAdapter(){
    	public void onKeyUp(Widget sender, char keyCode, int modifiers){
    		if (keyCode == KeyboardListenerAdapter.KEY_ENTER){
    			requestCheckSite();
    		}
    	}
    });
    
    //TODO change these into CSS-Styles
    resultTA.setStyleName("gwt-FlexTable");
    resultTA.setCellPadding(5);
    resultTA.setCellSpacing(0);
    
    resultTA.setTitle("Results");
    
    //TODO decide wich method to sort the columns
//    Hyperlink fileHL = new Hyperlink();
//    fileHL.setText("File");
//    fileHL.addClickListener(new ClickListener(){
//    	public void onClick(Widget sender){
//    		System.out.println("Here we have to sort the column!!");
//    	}
//    });
    
    upFileBT.addClickListener(new ClickListener(){
    	public void onClick(Widget sender){
    		if (upFileBT.getText().equals("File (desc)")){
    			upFileBT.setText("File (asc)");
    			//sort ascending
    			resultTA.sortAsString(0, SortDirectionET.ascending);
    		}else{
    			upFileBT.setText("File (desc)");
    			//sort descending
    			resultTA.sortAsString(0, SortDirectionET.descending);
    		}
    	}
    });
    
    //resultTA.setText(0, 0, "File");
    resultTA.setWidget(0, 0, upFileBT);
    resultTA.getFlexCellFormatter().setStyleName(0, 0, "resultHeading");  
    resultTA.setText(0, 1, "State");
    resultTA.getFlexCellFormatter().setStyleName(0, 1, "resultHeading");  
    resultTA.setText(0, 2, "Size in KB");
    resultTA.getFlexCellFormatter().setStyleName(0, 2, "resultHeading");  
    resultTA.setText(0, 3, "Time in sec");
    resultTA.getFlexCellFormatter().setStyleName(0, 3, "resultHeading");
    
    
    // Assume that the host HTML has elements defined whose
    // IDs are "slot1", "slot2".  In a real app, you probably would not want
    // to hard-code IDs.  Instead, you could, for example, search for all 
    // elements with a particular CSS class and replace them with widgets.
    //TODO change widget insertion to CSS class search
    RootPanel.get("CheckButton").add(button);
    RootPanel.get("UriTF").add(UriTB);
    RootPanel.get("Result").add(resultTA);
  }

	private void requestCheckSite() {
		resultTA.setText(1,0,"PLEASE WAIT...");
		UriTB.setEnabled(false);
		button.setEnabled(false);
		resultTA.getFlexCellFormatter().setStyleName(1,0,"wait");
		CheckResultProvider provider = new CheckResultProvider(this);
		provider.getCheckResult(UriTB.getText());
	}

	public void showCheckResult(CheckResult result){
		for (int i=resultTA.getRowCount()-1; i>0;i--){
			resultTA.removeRow(i);			
		}
		addResultToTable(result);
		UriTB.setEnabled(true);
		button.setEnabled(true);
	}
	
	private void addResultToTable(CheckResult checkResult) {
		int rowCount=resultTA.getRowCount();
		resultTA.setText(rowCount, 0, checkResult.getURI());
		resultTA.getFlexCellFormatter().setStyleName(rowCount, 0, "resultCell");
		resultTA.setText(rowCount, 1, checkResult.getState().toString());
		resultTA.getFlexCellFormatter().setStyleName(rowCount, 1, "resultCell");
		resultTA.setText(rowCount, 2, ""+checkResult.getSiteSizeKB());
		resultTA.getFlexCellFormatter().setStyleName(rowCount, 2, "resultCell");
		resultTA.setText(rowCount, 3, ""+checkResult.getTimeInSec());
		resultTA.getFlexCellFormatter().setStyleName(rowCount, 3, "resultCell");
		if (checkResult.getReferredFilesCheckResult() != null){
			for (int i=0; i< checkResult.getReferredFilesCheckResult().length; i++){
				addResultToTable(checkResult.getReferredFilesCheckResult()[i]);
			}
		}
	}
}
